package com.dailyblue.java.servlet.filter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// /*代表对所有请求拦截
// @WebFilter("/*")
public class FirstFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {

        // 拦截规则：对于什么请求拦截，什么请求放行
        int random = (int) (Math.random() * 100 + 1);
        System.out.println("产生的随机数是：" + random);
        if (random % 2 == 0) {
            System.out.println("是偶数，放行！");
            // 放行
            chain.doFilter(request, response);
        }
    }
}
